version: '3.9'

x-postgres-variables: &postgres-variables
  POSTGRES_DB: mt_db
  POSTGRES_HOST: db
  POSTGRES_PORT: 5432
  POSTGRES_USER: ${MT_POSTGRES_USER:-postgres}
  POSTGRES_PASSWORD: ${MT_POSTGRES_PASSWORD:-postgres}

x-django-variables: &django-variables
  DJANGO_PORT: 8000
  DEBUG: ${MT_DEBUG:-False}
  SECRET_KEY: ${MT_SECRET_KEY:-top_secret}
  FRONTEND_URL: ${MT_FRONTEND_URL:-localhost}
  DJANGO_SUPERUSER_PASSWORD: ${MT_DJANGO_SUPERUSER_PASSWORD:-password}
  DJANGO_SUPERUSER_USERNAME: ${MT_DJANGO_SUPERUSER_USERNAME:-admin}
  DJANGO_SUPERUSER_EMAIL: ${MT_DJANGO_SUPERUSER_EMAIL:-admin@email.com}

services:
  django:
    container_name: mt_django
    build: .
    image: medtime-back:${CI_COMMIT_SHORT_SHA:-latest}
    command: >
      sh -c "python manage.py migrate &&
             python manage.py collectstatic --noinput &&
             python manage.py runserver 0.0.0.0:8000 "
    environment:
      <<: [*postgres-variables, *django-variables]
    volumes:
      - django_static:/code/django_static/
      - django_media:/code/django_media/
    ports:
      - "18000:8000"
    expose:
      - "8000"
    restart: always
    depends_on:
      - db
    networks:
      - medtime_network

  db:
    container_name: mt_db
    image: postgres:15.2
    environment:
      <<: *postgres-variables
    ports:
      - "15433:5432"
    expose:
      - "5432"
    volumes:
      - pg_data:/var/lib/postgresql/data
    command: -p 5432
    networks:
      - medtime_network

  loyalty-rabbit:
    image: rabbitmq:3.9-management
    container_name: loyalty-rabbit
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
      - rabbitmq_log:/var/log/rabbitmq
    logging:
      driver: none
    restart: always
    ports:
      - 5672:5672
    networks:
      - medtime_network

  beat:
    restart: always
    environment:
      <<: [*postgres-variables,*django-variables]
    build:
      context: .
    entrypoint: [ "celery", "-A", "medtime", "beat","-l", "info", "--scheduler", "django_celery_beat.schedulers:DatabaseScheduler" ]
    depends_on:
      - db
      - loyalty-rabbit
      - django
      - worker
    networks:
      - medtime_network
  worker:
    restart: always
    environment:
      <<: [*postgres-variables,*django-variables]
    build:
      context: .
    entrypoint: [ "celery", "-A", "medtime.celery:app", "worker" ]
    depends_on:
      - db
      - loyalty-rabbit
      - django
    networks:
      - medtime_network
volumes:
  django_static:   
  django_media:
  pg_data:
  rabbitmq_data:
  rabbitmq_log:

networks:
  medtime_network:
    name: medtime_network
    attachable: true
